#!/usr/bin/perl
use DBI;

our $serverip;
our $serverport;
our $rrdfilepath; 
our $rrdimgpath;
our $htmlfilepath;
our $mumonitorpath;
our $mumonitorcmdpath;
our $rrdtoolcmd;
our $db_hostname;
our $db_port;
our $db_name;
our $dsn;
our $username;
our $password;
our $SMTP_SERVER;
our $SENDER; 
our $bandwidth_in_mib;
our $bandwidth_out_mib;
our $bandwidth_2_in_mib;
our $bandwidth_2_out_mib;
our $total_swap_mib;
our $avail_swap_mib;
our $total_ram_mib;
our $avail_ram_mib;
our $load_1min_mib;
our $load_5min_mib;
our $load_15min_mib;
our $cpu_user_mib;
our $cpu_system_mib;
our $tcp_connection_mib;
our $update_mib;
our @rrd_name1;
our @rrd_name2;
our @application_list;

if ( -f "/www/mumonitor/monitor/mumonitor.conf") {
        require "/www/mumonitor/monitor/mumonitor.conf";
} else {
        dir("Mumonitor need configuration file not found! .\n");
}

chomp(my $now_timestamp = `/bin/date +%s`);

$dbh = DBI->connect($dsn,$username,$password);
$sth1 = $dbh->prepare("select serverip,snmpcommunity from mu_server where bandwidth=1");
$sth1->execute();
while ( ($bandwidth_ip_monitor,$snmpcommunity) = $sth1->fetchrow_array() ) {
	chdir "$rrdfilepath$bandwidth_ip_monitor";
	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $bandwidth_ip_monitor $bandwidth_in_mib |");
	$ifInOct = <PROCESS>;
	close(PROCESS);
	$ifInOct =~ m/(.*)Counter32\: (.*)/;
	$ifInOctdata = $2;
	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $bandwidth_ip_monitor $bandwidth_out_mib |");
	$ifOutOct = <PROCESS>;
	close(PROCESS);
	$ifOutOct =~ m/(.*)Counter32\: (.*)/;
	$ifOutOctdata = $2;
	system ("$rrdtoolcmd update eth $now_timestamp:$ifInOctdata:$ifOutOctdata");
	my $rrd_file_name="$rrdfilepath$bandwidth_ip_monitor";
	my $rrd_img_name="$rrdimgpath$bandwidth_ip_monitor";
	system ("$mumonitorcmdpath/rrdgraph_eth.sh", "$rrd_file_name", "$rrd_img_name");
}

$sth2 = $dbh->prepare("select serverip,snmpcommunity from mu_server where sysinfo=1");
$sth2->execute();

while ( ($sysinfo_ip_monitor,$snmpcommunity) = $sth2->fetchrow_array() ) {
	chdir "$rrdfilepath$sysinfo_ip_monitor";
	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $total_swap_mib |");
	$memTotalSwap = <PROCESS>;
	close(PROCESS);
	$memTotalSwap =~ m/(.*)INTEGER\: (.*)/;
	$memTotalSwapdata = $2;
	system ("$rrdtoolcmd update mem_totalswap $now_timestamp:$memTotalSwapdata");

	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $avail_swap_mib |");
	$memAvailSwap = <PROCESS>;
	close(PROCESS);
	$memAvailSwap =~ m/(.*)INTEGER\: (.*)/;
	$memAvailSwapdata = $2;
	system ("$rrdtoolcmd update mem_availswap $now_timestamp:$memAvailSwapdata");

	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $total_ram_mib |");
	$memTotalReal = <PROCESS>;
	close(PROCESS);
	$memTotalReal =~ m/(.*)INTEGER\: (.*)/;
	$memTotalRealdata = $2;
	system ("$rrdtoolcmd update mem_totalreal $now_timestamp:$memTotalRealdata");

	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $avail_ram_mib |");
	$memAvailReal = <PROCESS>;
	close(PROCESS);
	$memAvailReal =~ m/(.*)INTEGER\: (.*)/;
	$memAvailRealdata = $2;
	system ("$rrdtoolcmd update mem_availreal $now_timestamp:$memAvailRealdata");

	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $tcp_connection_mib |");
	$TcpConn = <PROCESS>;
	close(PROCESS);
	$TcpConn =~ m/(.*)Gauge32\: (.*)/;
	$TcpConndata = $2;
	system ("$rrdtoolcmd update tcpconnection $now_timestamp:$TcpConndata");

	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $cpu_user_mib |");
	$CpuUser = <PROCESS>;
	close(PROCESS);
	$CpuUser =~ m/(.*)INTEGER\: (.*)/;
	$CpuUserdata = $2;
	system ("$rrdtoolcmd update cpu_user $now_timestamp:$CpuUserdata");

	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $cpu_system_mib |");
	$CpuSystem = <PROCESS>;
	close(PROCESS);
	$CpuSystem =~ m/(.*)INTEGER\: (.*)/;
	$CpuSystemdata = $2;
	system ("$rrdtoolcmd update cpu_system $now_timestamp:$CpuSystemdata");

	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $load_1min_mib |");
	$Load1min = <PROCESS>;
	close(PROCESS);
	$Load1min =~ m/(.*)STRING\: (.*)/;
	$Load1mindata = $2;
	system ("$rrdtoolcmd update load_1min $now_timestamp:$Load1mindata");

	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $load_5min_mib |");
	$Load5min = <PROCESS>;
	close(PROCESS);
	$Load5min =~ m/(.*)STRING\: (.*)/;
	$Load5mindata = $2;
	system ("$rrdtoolcmd update load_5min $now_timestamp:$Load5mindata");

	open(PROCESS,"snmpwalk -v 1 -c $snmpcommunity $sysinfo_ip_monitor $load_15min_mib |");
	$Load15min = <PROCESS>;
	close(PROCESS);
	$Load15min =~ m/(.*)STRING\: (.*)/;
	$Load15mindata = $2;
	system ("$rrdtoolcmd update load_15min $now_timestamp:$Load15mindata");

	my $rrd_file_name="$rrdfilepath$sysinfo_ip_monitor";
	my $rrd_img_name="$rrdimgpath$sysinfo_ip_monitor";
	system ("$mumonitorcmdpath/rrdgraph_mem.sh", "$rrd_file_name", "$rrd_img_name");
	system ("$mumonitorcmdpath/rrdgraph_tcp.sh", "$rrd_file_name", "$rrd_img_name");
	system ("$mumonitorcmdpath/rrdgraph_cpu.sh", "$rrd_file_name", "$rrd_img_name");
	system ("$mumonitorcmdpath/rrdgraph_load.sh", "$rrd_file_name", "$rrd_img_name");
}
$dbh->disconnect;
